package com.mcxx.modules.base.service.read;

import cn.hutool.core.date.DateUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.convert.ActivitiConvert;
import com.mcxx.modules.base.dao.read.ActivitiReadDao;
import com.mcxx.modules.base.dao.read.ActivitiRoleReadDao;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.order.dao.read.StandardReadDao;
import com.mcxx.modules.miniSalvation.order.dto.ActivitiDTO;
import com.mcxx.modules.miniSalvation.order.param.ActivitiParam;
import com.mcxx.modules.miniSalvation.order.param.QueryParam;
import com.mcxx.modules.system.dto.FinishRuleDTO;
import com.mcxx.modules.system.service.read.FinishRuleReadService;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.modules.temporary.order.dto.TmpOrderDTO;
import com.mcxx.modules.temporary.salary.service.read.TmpFamilySalaryReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.BaseRoleVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.query.QueryUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service("activitiReadService")
@TaTransactional(readOnly = true)
public class ActivitiReadService extends BaseService {
    @Autowired
    private ActivitiReadDao activitiReadDao;
    @Autowired
    private ActivitiRoleReadDao activitiRoleReadDao;
    @Autowired
    private FinishRuleReadService finishRuleReadService;
    @Autowired
    private TmpFamilySalaryReadService tmpFamilySalaryReadService;
    @Autowired
    private StandardReadDao standardReadDao;


    public String getOrderNextActviti(ActivitiParam activitiParam, List<BaseRoleVo> roleVoList){

        List<ActivitiDTO> activeList = activitiReadDao.getOrderNextState(activitiParam);
        if(CollectionUtils.isEmpty(activeList)){
            log.info("当前用户{}没有配置流程", activitiParam.getOperationName());
            throw new BizException("-1", "当前用户没有配置操作流程");
        }
        if(activeList.size() > 1){
            log.info("当前用户{}流程配置错误", activitiParam.getOperationName());
            throw new BizException("-1", "当前用户操作流程配置错误");
        }
        ActivitiDTO activitiDTO = activeList.get(0);

        if(null == activitiDTO){
            log.info("当前用户{}没有配置流程", activitiParam.getOperationName());
            throw new BizException("-1", "当前用户没有配置操作流程");
        }

        if(null == activitiDTO.getRoleLimit()){
            log.info("当前用户{}的角色限制配置错误", activitiParam.getOperationName());
            throw new BizException("-1", "当前用户的角色限制配置错误");
        }

        if("0".equals(activitiDTO.getRoleLimit())){

            return activitiDTO.getNextStep();
        }

        if("1".equals(activitiDTO.getRoleLimit())){
            long activitiId = activitiDTO.getId();
            List<String> roleIdList = roleVoList.stream().map(BaseRoleVo::getRoleId).collect(Collectors.toList());

            int cnt = activitiRoleReadDao.cntByActivitiIdAndRoleIds(activitiId, roleIdList);
            if(cnt <= 0){
                log.info("当前用户{}不具备操作权限", activitiParam.getOperationName());
                throw new BizException("-1", "当前用户不具备操作权限");
            }
        }

        return activitiDTO.getNextStep();
    }


    public String getNextStepForTmp(TmpAuditDTO tmpAuditDTO){

        String nextStep = exceedAudit(tmpAuditDTO);
        if(!CommonUtil.invalidArgs(nextStep)){
            return nextStep;
        }
        return null;




//            log.info("当前用户{}没有配置流程", activitiParam.getOperationName());



//            log.info("当前用户{}流程配置错误", activitiParam.getOperationName());









    }


    private String exceedAudit(TmpAuditDTO tmpAuditDTO){


        String standardMoneys = standardReadDao.getStandardMoney(tmpAuditDTO.getAreaCode().substring(0, 6)+ "000000", "500","500");
        //FinishRuleDTO finishRuleDTO = finishRuleReadService.getByAreaCodeAndStandType(tmpAuditDTO.getAreaCode().substring(0,6), null);
        Double standardMoney;
        if(null != standardMoneys){
            standardMoney = Double.parseDouble(standardMoneys);

            //double familySendMoney = tmpFamilySalaryReadService.getFamilySendMoney(tmpAuditDTO.getOrderId(), tmpAuditDTO.getFamilyId(), DateUtil.formatDate(DateUtil.beginOfYear(new Date())));

            double familySendMoney = tmpAuditDTO.getSalvMoney();

            if(familySendMoney > standardMoney){
                return Constant.BizStateTmpNew.WAITING_REPORT;
            }
        }else{
            throw new BizException("-1","当前账号没有配置审批限额标准,请先配置标准");
        }

        if(!CommonUtil.invalidArgs(tmpAuditDTO.getOnethingCheckType()) && Constant.OneThingCheckType.YES.equals(tmpAuditDTO.getOnethingCheckType())){
            return Constant.BizStateTmpNew.WAITING_REPORT;
        }
        return null;
    }

}
